package il.ac.technion.wiki.impl.search;

import il.ac.technion.wiki.api.search.NeutralWordsCleaner;

public class WikiNeutralWordsCleaner implements NeutralWordsCleaner {

	private String[] neutralWords = 
	{"is","a","the","and"};
	private String[] neutralSign =
	{",","\\.","!","-","	"};
	public WikiNeutralWordsCleaner() {
		
	}

	@Override
	public String cleanTitle(String q) {
		return regularCleanString(q);
	}

	@Override
	public String cleanDocumentContent(String doc) {
		return regularCleanString(doc);
	}

	@Override
	public String cleanTerm(String t) {
		return regularCleanString(t);
	}

	@Override
	public String regularCleanString(String s) {
		String regExp = buildRegExp(neutralSign);
		if (s == null)
			return null;
		String $ = s.replaceAll(regExp, " ");
		$ = $.replaceAll(" +", " ");
		String[] splitted = $.split(" ");
		$ = "";
		for(int i = 0; i < splitted.length; i++){
			if (splitted[i].equals(""))
				continue;
			if (!isContains(neutralWords,splitted[i])){
				$ = $ + splitted[i] + " ";
			}
		}
		if ($ == "")
			return $;
		$ = $.substring(0, $.length()-1);
		return $;
	}
	
	private String buildRegExp(String[] arr){
		String regExp = " ";
		for (int i = 0; i < arr.length; i++){
			regExp = regExp + "|" + arr[i];
		}
		return regExp;
	}
	
	private boolean isContains(String[] arr,String s){
		for (int i = 0; i < arr.length; i++){
			if (arr[i].equals(s))
				return true;
		}
		return false;
	}

}
